/*******************************************************************************
 * Team: R2-DTurrets
 * ECEN 4013 Senior Design 1
 *
 * Controller Code
 *
 * Authors: Matt Polito - Project Manager
 *          Mike Krueger - Lead Engineer
 *          Kevin Bright - Motion Master
 *          Shane Ptomey - Power Sage
 *          Kali McCuistion - Visual and Audio Doctor
 *          Christien Sager - Communication Analyst
 *          Wesley Taylor - Information Expert
*******************************************************************************/
// ***** External File Includes *****************************************************
#include <xc.h>
#include <pic16f886.h>
#include "global_defs.h"

// ***** PIC16f886 Configuration Bits ***********************************************
// CONFIG1
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// ***** Term Definition ************************************************************

    // *** Pin Definitions **********************************************************
    #define A0_Pin PORTAbits.RA0
    #define A1_Pin PORTAbits.RA1
    #define LCD_data PORTB
    #define LCD_en PORTCbits.RC1
    #define LCD_rw PORTCbits.RC2
    #define LCD_rs PORTCbits.RC3
    #define Fire_Button PORTCbits.RC4
    #define Rocker_Switch PORTCbits.RC5
    #define Blue_TX PORTCbits.RC6
    #define Blue_RX PORTCbits.RC7

    // *** LCD Definitions **********************************************************
        // Definitions for charaters to write to the LCD screen
    #define Line1  LCD_command(0x80);
    #define Line2  LCD_command(0xC0);
    #define Line3  LCD_command(0x94);
    #define Line4  LCD_command(0xD4);
    #define H LCD_senddata(0x48)
    #define e LCD_senddata(0x65)
    #define a LCD_senddata(0x61)
    #define l LCD_senddata(0x6c)
    #define t LCD_senddata(0x74)
    #define h LCD_senddata(0x68)
    #define colon LCD_senddata(0x3a)
    #define zero LCD_senddata(0x30)
    #define one LCD_senddata(0x31)
    #define nine LCD_senddata(0x39)
    #define eight LCD_senddata(0x38)
    #define seven LCD_senddata(0x37)
    #define six LCD_senddata(0x36)
    #define five LCD_senddata(0x35)
    #define four LCD_senddata(0x34)
    #define three LCD_senddata(0x33)
    #define blank LCD_senddata(0x10)
    #define A LCD_senddata(0x41)
    #define m LCD_senddata(0x6d)
    #define o LCD_senddata(0x6f)
    #define M LCD_senddata(0x4d)
    #define d LCD_senddata(0x64)
    #define S LCD_senddata(0x53)
    #define n LCD_senddata(0x6e)
    #define i LCD_senddata(0x69)
    #define p LCD_senddata(0x70)
    #define s LCD_senddata(0x73)
    #define B LCD_senddata(0x42)
    #define star LCD_senddata(0x2a)
    #define two LCD_senddata(0x32)
    #define period LCD_senddata(0x2e)
    #define T LCD_senddata(0x54)
    #define U LCD_senddata(0x55)
    #define R LCD_senddata(0x52)
    #define E LCD_senddata(0x45)
    #define g LCD_senddata(0x67)
    #define block LCD_senddata(0xFF)
    #define D LCD_senddata(0x44)
    #define C LCD_senddata(0x43)
    #define w LCD_senddata(0x77)
    #define P LCD_senddata(0x50)
    #define heart LCD_senddata(0x00)
    #define bullet LCD_senddata(0x01)
    #define skull1 LCD_senddata(0x02)
    #define skull2 LCD_senddata(0x03)
    #define skull3 LCD_senddata(0x04)
    #define skull4 LCD_senddata(0x05)
    #define skull5 LCD_senddata(0x06)
    #define skull6 LCD_senddata(0x07)

    // *** Joystick Definitions *****************************************************
    #define A0_Select   ANS0
    #define A0_Channel  0
    #define A1_Select   ANS1
    #define A1_Channel  1
    #define INPUT       1
    #define OUTPUT      0
    #define ANALOG      1
    #define DIGITAL     0
    #define xland 0x0f
    #define xlor 0x20
    #define xhor 0x10
    #define yland 0x0f
    #define ylor 0x40
    #define yhor 0x30

    // *** Bluetooth Definitions ****************************************************
    #define SPEN  RCSTAbits.SPEN
    #define CREN  RCSTAbits.CREN
    #define TXEN  TXSTAbits.TXEN
    #define SET         1
    #define CLEAR       0

// ***** Variable Declarations ******************************************************
unsigned int alive = 1;
unsigned int ammoCount = 10;
unsigned int health = 16;
unsigned int reload_counter = 0;
unsigned int cooldown_counter = 0;
unsigned char switch_flag = 0;
unsigned char reload_flag = 0;
unsigned char cooldown_flag = 0;

unsigned char c1 = 0x80;
unsigned char c2 = 0xc0;
unsigned char c3 = 0x94;
unsigned char c4 = 0xd4;

unsigned int rc = 0;
// ***** Function Declarations ******************************************************
void setBaudRate();
void setFOSC(void);
void fire(void);
void snipeMode(void);
void blastMode(void);
void hit(void);
void lcdInit(void);
void lcdWrite(void);
void ADCInit(void);
void LCD_senddata(unsigned char var);
void LCD_command(unsigned char var);
void heartCreate(void);
void bulletCreate(void);
void skullCreate1(void);
void skullCreate2(void);
void skullCreate3(void);
void skullCreate4(void);
void skullCreate5(void);
void skullCreate6(void);
void dead1(void);
void shiftleft(void);
void shiftright(void);
void moveleft(void);
void moveright(void);
void heal(void);

unsigned char xLow(unsigned int xAxis);
unsigned char xHigh(unsigned int xAxis);
unsigned char yLow(unsigned int yAxis);
unsigned char yHigh(unsigned int yAxis);
unsigned char ADCRead8bit(const unsigned char channel);

// ***** Main Loop ******************************************************************

int main(void)
{
    __delay_ms(1000);

    // ***** Local Variables ********************************************************
    unsigned char xAxis;
    unsigned char yAxis;

    setFOSC();      //Sets FOSC to 8MHz
    setBaudRate();  //Sets BaudRate 57600 bps
    ADCInit();      //Initialize ADC

    //Configure I/O Pins
    TRISAbits.TRISA0 = 1;
    TRISAbits.TRISA1 = 1;
    TRISAbits.TRISA4 = 0;
    TRISAbits.TRISA5 = 1;

    TRISB = 0;
    TRISCbits.TRISC1 = 0;
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    TRISCbits.TRISC4 = 1;
    TRISCbits.TRISC5 = 1;
    TRISCbits.TRISC6 = 0;
    TRISCbits.TRISC7 = 1;

    //Configure Pin Use
    SSPCONbits.SSPEN = 0;
    T1CONbits.TMR1ON = 0;       //Verify timer is off on RC1

    // ***** Initialize Controller **************************************************
    
    lcdInit();
    heartCreate();//Initialize LCD with write commands
    bulletCreate();
    skullCreate1();
    skullCreate2();
    skullCreate3();
    skullCreate4();
    skullCreate5();
    skullCreate6();
    lcdWrite();
     //Writes initial format for LCD

    //Initialize Analog
    A0_Select = ANALOG;         // make analog
    A1_Select = ANALOG;

    //Declaring Bluetooth Outputs
    SPEN = SET;
    CREN = SET;
    TXEN = SET;
    
    PORTAbits.RA4 = 0;
    while(alive == 1)
    {
        CREN = 0;
        CREN = 1;

        while((alive == 1) && (Fire_Button != 1) && (cooldown_flag != 1) && (reload_flag != 1))
        {
            TXREG = 0x00;
            
            if(RCREG == 0xab)
            {
                PORTAbits.RA4 = 1;
                hit();

                __delay_ms(100);
                __delay_ms(100);
                __delay_ms(100);
                 __delay_ms(100);

                PORTAbits.RA4 = 0;
            }

            if(RCREG == 0x99){
                heal();
            }
            
            xAxis = ADCRead8bit(A0_Channel); //8 bit number
            yAxis = ADCRead8bit(A1_Channel);
            TXREG = xLow(xAxis);
            TXREG = xHigh(xAxis);
            TXREG = yLow(yAxis);
            TXREG = yHigh(yAxis);

            if(switch_flag == 1){
                if(Rocker_Switch == 0){
                    switch_flag = 0;
                    blastMode();
                }
            }
            if(switch_flag == 0){
                if(Rocker_Switch == 1){
                    switch_flag = 1;
                    snipeMode();
                }
            }
           
        }

// Cooldown Subroutine
        while(cooldown_flag == 1){
            if(RCREG == 0xab)
            {
                PORTAbits.RA4 = 1;
                hit();
            }
            if(RCREG == 0x99)
            {
                heal();
            }

            __delay_ms(1);
            cooldown_counter++;

            if(switch_flag == 1){
                if(Rocker_Switch == 0){
                    switch_flag = 0;
                    blastMode();
                }
            }
            if(switch_flag == 0){
                if(Rocker_Switch == 1){
                    switch_flag = 1;
                    snipeMode();
                }
            }
            if(cooldown_counter == 700){
                LCD_command(0xd9);
                blank;C;o;o;l;d;o;w;n;blank;
                __delay_ms(10);
                TXREG = 0x88;
                __delay_ms(10);
                TXREG = 0x00;
            } if(cooldown_counter == 1400){
                TXREG = 0x55;
                __delay_ms(10);
                TXREG = 0x00;
                
            } if(cooldown_counter >= 1800){
                TXREG = 0x00;
                __delay_ms(10);
                cooldown_flag = 0;
                cooldown_counter = 0;
                LCD_command(0xd9);
                T;U;R;R;E;T;blank;two;period;zero;
                PORTAbits.RA4 = 0;
            }
        }

// Reload Subroutine
        while(reload_flag == 1){
            if(RCREG == 0xab)
            {
                
                hit();
                PORTAbits.RA4 = 1;
                
            }
            if(RCREG == 0x99)
            {
                heal();
            }

            __delay_ms(1);
            reload_counter++;

            if(switch_flag == 1){
                if(Rocker_Switch == 0){
                    switch_flag = 0;
                    blastMode();
                }
            }
            if(switch_flag == 0){
                if(Rocker_Switch == 1){
                    switch_flag = 1;
                    snipeMode();
                }
            }
            
            if(reload_counter == 300){
                LCD_command(0xd9);
                R;e;l;o;a;d;i;n;g;blank;
                TXREG = 0x00;
            }
            else if(reload_counter == 500){
                LCD_command(0xc6);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
                
            }
            else if(reload_counter == 1000){
                LCD_command(0xc7);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 1500){
                LCD_command(0xc8);
                bullet;
                TXREG = 0x66;
                __delay_ms(10);
                TXREG = 0x00;
                PORTAbits.RA4 = 0;
                
                
            }
            else if(reload_counter == 2000){
                LCD_command(0xc9);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 2500){
                LCD_command(0xca);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 3000){
                LCD_command(0xcb);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 3500){
                LCD_command(0xcc);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 4000){
                LCD_command(0xcd);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter == 4500){
                LCD_command(0xce);
                bullet;
                TXREG = 0x77;
                __delay_ms(10);
                TXREG = 0x00;
            }
            else if(reload_counter >= 5000){
                reload_flag = 0;
                cooldown_flag = 0;
                reload_counter = 0;
                ammoCount = 10;
                LCD_command(0xcf);
                bullet;
                TXREG = 0x00;
                __delay_ms(10);
                LCD_command(0xd9);
                T;U;R;R;E;T;blank;two;period;zero;
            }
        }

        if(Fire_Button == 1){
           if(Rocker_Switch == 1)
           {
               TXREG = 0xff;
               
               //Send Snipe IR call
           }
           if(Rocker_Switch == 0)
           {
               ammoCount = 1;
               TXREG = 0xee;
               //Send Blast IR call
           }
           __delay_ms(10);
           TXREG = 0x00;
           fire();
           
        }
    } // end main loop

    LCD_command(0x01);

    while(alive != 1){

        LCD_command(c1);
        skull1;skull2;skull3;
        LCD_command(c2);
        skull4;skull5;skull6;

        __delay_ms(100);

        LCD_command(c3 + 17);
        skull1;skull2;skull3;
        LCD_command(c4 + 17);
        skull4;skull5;skull6;

        __delay_ms(100);
 
        LCD_command(c3);
        skull1;skull2;skull3;
        LCD_command(c4);
        skull4;skull5;skull6;

        __delay_ms(100);

        LCD_command(c1 + 17);
        skull1;skull2;skull3;
        LCD_command(c2 + 17);
        skull4;skull5;skull6;

        __delay_ms(100);

        while(1){
            dead1();
            
            
        }
       
        //DEAD
    }
    return 0;
} // end main

// ***** Function Definitions *******************************************************

// ***** Initialization Functions ***************************************************
void setFOSC() //Initialize Oscillator
{
    OSCCON = 0b01110101; // Fosc = 8 MHz, OSTS = 0, HFINTOSC, Internal Osc
}

void setBaudRate() //Configure Bluetooth Baud Rate
{
   TXSTAbits.SYNC = 0;  //Set Eusart to Asynchronous Mode
   TXSTAbits.BRGH = 1;  //Low speed baud rate select
   BAUDCTLbits.BRG16 = 0; //8-bit baud rate generator selected
   SPBRG = 8; //Value for 57600 baud rate
}

void lcdInit(void) //Initialize LCD Screen
{
    __delay_ms(300);
   // INITIALIZE
   LCD_data = 0b00111000;  //Function set: 2 Line, 8-bit, 5x7 dots
   LCD_rs = 0;  //Selected command register
   LCD_rw = 0;
   LCD_en = 1;  //Enable H->L
   LCD_en = 0;
   __delay_ms(10);  //Wait for LCD to process the command

   __delay_ms(300);
   LCD_data = 0b00001100; //Display on
   LCD_rs = 0;  //Selected command register
   LCD_en = 1;  //Enable H->L
   LCD_en = 0;
   __delay_ms(10);

   __delay_ms(300);
   LCD_data = 0b00000001;  //Clear  LCD
   LCD_rs = 0;  //Selected command register
   LCD_en = 1;  //Enable H->L
   LCD_en = 0;
   __delay_ms(10);//Wait for LCD to process the command

   __delay_ms(300);
   LCD_data = 0b00000110; //Entry mode, auto increment with no shift
   LCD_rs = 0; //Selected command register
   LCD_en = 1; //Enable H->L
   LCD_en = 0;
   __delay_ms(10);
}

void lcdWrite(void)  //Write Template for Use
{
    Line1;
    H;P;colon;blank;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;heart;
    __delay_ms(10);block;
    Line2;
    A;m;m;o;colon;blank;bullet;bullet;bullet;bullet;bullet;bullet;bullet;bullet;bullet;bullet;
    __delay_ms(10);

 


    if(Rocker_Switch == 0){
        Line3;
        M;o;d;e;colon;blank;B;l;a;s;t;
    }else if(Rocker_Switch == 1){
        Line3;
        M;o;d;e;colon;blank;S;n;i;p;e;
    }
    __delay_ms(10);
    Line4;
    blank;blank;star;star;star;T;U;R;R;E;T;blank;two;period;zero;star;star;star;
    __delay_ms(10);
}

void ADCInit(void) //Initialize Analog for Joystick
{
    ADFM = 0;  // Left Justified
    ADCON0bits.ADCS = 0;  // clock FOSC/2
    VCFG0 = 0; // Use Vdd as Reference
    VCFG1 = 0; // Use ground
    ADON = 1;  // ADC ON
}

// ***** Joystick Functions *********************************************************
unsigned char xLow(unsigned int xAxis){
    int xAxisLow;
    xAxisLow = (xAxis & xland);
    xAxisLow |= xlor;
    return xAxisLow;
}
unsigned char xHigh(unsigned int xAxis){
    int xAxisHigh;
    xAxisHigh = (xAxis >> 4);
    xAxisHigh |= xhor;
    return xAxisHigh;
}

unsigned char yLow(unsigned int yAxis){
    int yAxisLow;
    yAxisLow = (yAxis & yland);
    yAxisLow |= ylor;
    return yAxisLow;
}

unsigned char yHigh(unsigned int yAxis){
    int yAxisHigh;
    yAxisHigh = (yAxis >> 4);
    yAxisHigh |= yhor;
    return yAxisHigh;
}

// ***** LCD Functions **************************************************************
void LCD_command(unsigned char var)
{
    LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots
    LCD_rs = 0;     //Selected command register
    LCD_rw = 0;     //We are writing in instruction register
    LCD_en = 1;     //Setting Enable Pin H->L writes to LCD
    LCD_en = 0;
    __delay_ms(10);     //Wait for LCD to process the command
}

void LCD_senddata(unsigned char var)
{
    LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots
    LCD_rs = 1;     //Selected data register
    LCD_rw = 0;     //We are writing
    LCD_en = 1;     //Setting Enable Pin H->L writes to LCD
    LCD_en = 0;
    __delay_ms(10);     //Wait for LCD to process the command
}

void dead1(void){
    TXREG = 0xbb;
    LCD_command(c1+8);
    D;E;A;D;
    TXREG = 0xbb;

    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
     moveleft();
     moveleft();
     moveleft();
     moveleft();
     moveleft();
     TXREG = 0xbb;

    LCD_command(c1+8);
    blank;
    LCD_command(c2+8);
    D;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    LCD_command(c1+9);
    blank;
    LCD_command(c2+8);
    blank;
    LCD_command(c3+8);
    D;
    LCD_command(c2+9);
    E;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    LCD_command(c1+10);
    blank;
    LCD_command(c2+9);
    blank;
    LCD_command(c3+8);
    blank;
    LCD_command(c4+8);
    D;
    LCD_command(c3+9);
    E;
    LCD_command(c2+10);
    A;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    LCD_command(c1+11);
    blank;
    LCD_command(c2+10);
    blank;
    LCD_command(c3+9);
    blank;
    LCD_command(c4+9);
    E;
    LCD_command(c3+10);
    A;
    LCD_command(c2+11);
    D;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    LCD_command(c2+11);
    blank;
    LCD_command(c3+10);
    blank;

    TXREG = 0xbb;
    LCD_command(c4+10);
    A;
    LCD_command(c3+11);
    D;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    LCD_command(c3+11);
    blank;

    LCD_command(c4+11);
    D;

    TXREG = 0xbb;
    __delay_ms(100);
    __delay_ms(100);
    __delay_ms(100);

    TXREG = 0xbb;
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();
    moveright();

    LCD_command(c3+8);
    D;E;A;D;
    LCD_command(c4+8);
    blank;blank;blank;blank;

    __delay_ms(100);



    LCD_command(c2+8);
    D;E;A;D;
    LCD_command(c3+8);
    blank;blank;blank;blank;

    __delay_ms(100);



    LCD_command(c1+8);
    D;E;A;D;
    LCD_command(c2+8);
    blank;blank;blank;blank;

    __delay_ms(100);

    moveleft();
    moveleft();
    moveleft();
    moveleft();
    moveleft();

        

}

void moveright(void){

LCD_command(c4 + 8);
         blank;
         shiftright();
         LCD_command(c4+8);
         D;E;A;D;
         __delay_ms(100);
        __delay_ms(100);
         __delay_ms(100);

 }

void moveleft(void){
 LCD_command(c1 + 11);
         blank;
         shiftleft();
         LCD_command(c1+8);
         D;E;A;D;
         __delay_ms(100);
        __delay_ms(100);
         __delay_ms(100);


}

void shiftleft(void){

    c1--;
    c2--;
    c3--;
    c4--;

}
void shiftright(void){

    c1++;
    c2++;
    c3++;
    c4++;

}

void heartCreate(void)
{
    LCD_command(0x40);
    LCD_senddata(0x00);

    LCD_command(0x41);
    LCD_senddata(0x1b);

    LCD_command(0x42);
    LCD_senddata(0x1f);

    LCD_command(0x43);
    LCD_senddata(0x1f);

    LCD_command(0x44);
    LCD_senddata(0x0e);

    LCD_command(0x45);
    LCD_senddata(0x04);

    LCD_command(0x46);
    LCD_senddata(0x00);

    LCD_command(0x47);
    LCD_senddata(0x00);
}

void bulletCreate(void){
    LCD_command(0x48);
    LCD_senddata(0x0e);

    LCD_command(0x49);
    LCD_senddata(0x0e);

    LCD_command(0x4a);
    LCD_senddata(0x0e);

    LCD_command(0x4b);
    LCD_senddata(0x0e);

    LCD_command(0x4c);
    LCD_senddata(0x0e);

    LCD_command(0x4d);
    LCD_senddata(0x0e);

    LCD_command(0x4e);
    LCD_senddata(0x0e);

    LCD_command(0x4f);
    LCD_senddata(0x0e);
}

void skullCreate1(void)
{
    LCD_command(0x50);
    LCD_senddata(0x00);

    LCD_command(0x51);
    LCD_senddata(0x02);

    LCD_command(0x52);
    LCD_senddata(0x02);

    LCD_command(0x53);
    LCD_senddata(0x0f);

    LCD_command(0x54);
    LCD_senddata(0x03);

    LCD_command(0x55);
    LCD_senddata(0x00);

    LCD_command(0x56);
    LCD_senddata(0x00);

    LCD_command(0x57);
    LCD_senddata(0x00);
    // END FIRST CHARACTER
}

void skullCreate2(void){

    LCD_command(0x58);
    LCD_senddata(0x00);

    LCD_command(0x59);
    LCD_senddata(0x00);

    LCD_command(0x5a);
    LCD_senddata(0x00);

    LCD_command(0x5b);
    LCD_senddata(0x0e);

    LCD_command(0x5c);
    LCD_senddata(0x1f);

    LCD_command(0x5d);
    LCD_senddata(0x15);

    LCD_command(0x5e);
    LCD_senddata(0x1f);

    LCD_command(0x5f);
    LCD_senddata(0x1f);
    //END SECOND CHAR
}

void skullCreate3(void){


    LCD_command(0x60);
    LCD_senddata(0x00);

    LCD_command(0x61);
    LCD_senddata(0x08);

    LCD_command(0x62);
    LCD_senddata(0x08);

    LCD_command(0x63);
    LCD_senddata(0x1e);

    LCD_command(0x64);
    LCD_senddata(0x18);

    LCD_command(0x65);
    LCD_senddata(0x00);

    LCD_command(0x66);
    LCD_senddata(0x00);

    LCD_command(0x67);
    LCD_senddata(0x00);
    //END THIRD CHAR
}

void skullCreate4(void)
{
    LCD_command(0x68);
    LCD_senddata(0x03);

    LCD_command(0x69);
    LCD_senddata(0x0f);

    LCD_command(0x6a);
    LCD_senddata(0x02);

    LCD_command(0x6b);
    LCD_senddata(0x02);

    LCD_command(0x6c);
    LCD_senddata(0x00);

    LCD_command(0x6d);
    LCD_senddata(0x00);

    LCD_command(0x6e);
    LCD_senddata(0x00);

    LCD_command(0x6f);
    LCD_senddata(0x00);
    // END FIRST CHARACTER
}

void skullCreate5(void){

    LCD_command(0x70);
    LCD_senddata(0x0e);

    LCD_command(0x71);
    LCD_senddata(0x0e);

    LCD_command(0x72);
    LCD_senddata(0x00);

    LCD_command(0x73);
    LCD_senddata(0x00);

    LCD_command(0x74);
    LCD_senddata(0x00);

    LCD_command(0x75);
    LCD_senddata(0x00);

    LCD_command(0x76);
    LCD_senddata(0x00);

    LCD_command(0x77);
    LCD_senddata(0x00);
    //END SECOND CHAR
}

void skullCreate6(void){


    LCD_command(0x78);
    LCD_senddata(0x18);

    LCD_command(0x79);
    LCD_senddata(0x1e);

    LCD_command(0x7a);
    LCD_senddata(0x08);

    LCD_command(0x7b);
    LCD_senddata(0x08);

    LCD_command(0x7c);
    LCD_senddata(0x00);

    LCD_command(0x7d);
    LCD_senddata(0x00);

    LCD_command(0x7e);
    LCD_senddata(0x00);

    LCD_command(0x7f);
    LCD_senddata(0x00);
    //END THIRD CHAR
}

void fire()
{
    ammoCount--;                    // Reduce ammo in turret due to firing
    switch (ammoCount)              // Display corresponding ammo count to LCD
    {
        case 9 :
            LCD_command(0xcf);
            blank;
            break;
        case 8 :
            LCD_command(0xce);
            blank;
            break;
        case 7 :
            LCD_command(0xcd);
            blank;
            break;
        case 6 :
            LCD_command(0xcc);
            blank;
            break;
        case 5 :
            LCD_command(0xcb);
            blank;
            break;
        case 4 :
            LCD_command(0xca);
            blank;
            break;
        case 3 :
            LCD_command(0xc9);
            blank;
            break;
        case 2 :
            LCD_command(0xc8);
            blank;
            break;
        case 1 :
            LCD_command(0xc7);
            blank;
            break;
        case 0 :
            LCD_command(0xc6);
            blank;blank;blank;blank;blank;blank;blank;blank;blank;blank;
            break;
        default :
            break;
    }
    if (ammoCount == 0)                 // Reload if ammo depleted
    {
        if(Rocker_Switch == 0){
            LCD_command(0xd9);
            blank;B;o;o;o;o;o;o;m;blank;
        }
        if(Rocker_Switch == 1){
            LCD_command(0xd9);
            blank;P;e;w;blank;blank;P;e;w;blank;
        }
        reload_flag = 1;
    }else{                              // Cooldown if ammo not depleted
        LCD_command(0xd9);
        blank;P;e;w;blank;blank;P;e;w;blank;
        cooldown_flag = 1;
    }
}

void snipeMode(void)
{
    LCD_command(0x9a);
    S;n;i;p;e;
}

void blastMode(void)
{
    LCD_command(0x9a);
    B;l;a;s;t;
}

void hit(void)
{
    health--;                       // Reduce health and display on LCD screen
    switch (health)
    {
        case 15 :
            LCD_command(0x93);
            blank;
            __delay_ms(10);
            break;
        case 14 :
            LCD_command(0x92);
            blank;
            __delay_ms(10);
            break;
        case 13 :
            LCD_command(0x91);
            blank;
            __delay_ms(10);
            break;
        case 12 :
            LCD_command(0x90);
            blank;
            __delay_ms(10);
            break;
        case 11 :
            LCD_command(0x8F);
            blank;
            __delay_ms(10);
            break;
        case 10 :
            LCD_command(0x8E);
            blank;
            __delay_ms(10);
            break;
        case 9 :
            LCD_command(0x8D);
            blank;
            __delay_ms(10);
            break;
        case 8 :
            LCD_command(0x8C);
            blank;
            __delay_ms(10);
            break;
        case 7 :
            LCD_command(0x8B);
            blank;
            __delay_ms(10);
            break;
        case 6 :
            LCD_command(0x8A);
            blank;
            __delay_ms(10);
            break;
        case 5 :
            LCD_command(0x89);
            blank;
            __delay_ms(10);
            break;
        case 4 :
            LCD_command(0x88);
            blank;
            __delay_ms(10);
            break;
        case 3 :
            LCD_command(0x87);
            blank;
            __delay_ms(10);
            break;
        case 2 :
            LCD_command(0x86);
            blank;
            __delay_ms(10);
            break;
        case 1 :
            LCD_command(0x85);
            blank;
            __delay_ms(10);
            break;
        case 0 :
            TXREG = 0xbb;
            alive = 0;
            __delay_ms(10);
            TXREG = 0x00;
            break;
        default :
            break;
    }
}
void heal(void)
{
    if(health < 16 ){
        health++;
    }
    // Increase health and display on LCD screen
    switch (health)
    {
        case 16 :
            LCD_command(0x93);
            heart;
            __delay_ms(10);
            break;
        case 15 :
            LCD_command(0x92);
            heart;
            __delay_ms(10);
            break;
        case 14 :
            LCD_command(0x91);
            heart;
            __delay_ms(10);
            break;
        case 13 :
            LCD_command(0x90);
            heart;
            __delay_ms(10);
            break;
        case 12 :
            LCD_command(0x8F);
            heart;
            __delay_ms(10);
            break;
        case 11 :
            LCD_command(0x8E);
            heart;
            __delay_ms(10);
            break;
        case 10 :
            LCD_command(0x8D);
            heart;
            __delay_ms(10);
            break;
        case 9 :
            LCD_command(0x8C);
            heart;
            __delay_ms(10);
            break;
        case 8 :
            LCD_command(0x8B);
            heart;
            __delay_ms(10);
            break;
        case 7 :
            LCD_command(0x8A);
            heart;
            __delay_ms(10);
            break;
        case 6 :
            LCD_command(0x89);
            heart;
            __delay_ms(10);
            break;
        case 5 :
            LCD_command(0x88);
           heart;
            __delay_ms(10);
            break;
        case 4 :
            LCD_command(0x87);
            heart;
            __delay_ms(10);
            break;
        case 3 :
            LCD_command(0x86);
            heart;
            __delay_ms(10);
            break;
        case 2 :
            LCD_command(0x85);
            heart;
            __delay_ms(10);
            break;
        default :
            break;
    }
}

// ****** Read ADC 8 bit ************************************************************
unsigned char ADCRead8bit(const unsigned char channel)
{
    ADCON0bits.CHS = channel;   //Set the channel
    __delay_us(5);              //Delay
    GO_nDONE = 1;               //Start the conversion
    while(GO_nDONE == 1);       //Wait for the conversion to finish
    return ADRESH;              // Return the result
}